<?php

namespace app\common\service\GoodsService;

use app\model\BbcModel\Goods;
use \app\model\BbcModel\GoodsTags;
use app\Normal;
use think\Db;

class GoodsTagsService
{
    public function tagLists($param) : array{
        $page = (isset($param['page']) && $param['page'] != '') ? $param['page'] : 1;
        $limit = (isset($param['limit']) && $param['limit'] != '') ? $param['limit'] : 20;
        $where = [
            ['cid','=',$param['cid']],
        ];
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['type','=',$param['type']];
        }
        if(isset($param['name']) && $param['name'] != ''){
            $where[] = ['name','like','%'.trim($param['name']).'%'];
        }
        $data_lists = GoodsTags::instance()->where($where)->page((int)$page)->limit((int)$limit)->field('id,type,name,desc')->order('id desc')->select();
        foreach ($data_lists as $key=>$val){
            if($val['type'] == 0){ //供应商标签
                $data_lists[$key]['goods_num'] = Goods::instance()->where(['cid'=>$param['cid']])->where('find_in_set(:supply_tag,supply_tag)',['supply_tag'=>$val['id']])->count(); //供应商标签关联商品数
            }else{
                $data_lists[$key]['goods_num'] = Goods::instance()->where(['cid'=>$param['cid']])->where('find_in_set(:tag,tag)',['tag'=>$val['id']])->count();  //自定义标签关联数
            }
        }
        $total = GoodsTags::instance()->where($where)->count();
        return Normal::returnCode(Normal::SUC,'获取成功',['data_lists'=>$data_lists,'total'=>$total]);
    }

    public function tagsInfo($param) : array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'标签ID不能为空');
        }
        $info = GoodsTags::instance()->info(['id'=>$param['id'],'cid'=>$param['cid'],'_fields_'=>'id,name,desc']);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'标签不存在');
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$info);
    }

    public function editTags($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'标签ID不能为空');
        }
        if(!isset($param['desc']) || !$param['desc']){
            return Normal::returnCode(Normal::FAIL,'标签描述不能为空');
        }
        $info = GoodsTags::instance()->info(['id'=>$param['id'],'cid'=>$param['cid']]);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'标签不存在');
        }
        $res = GoodsTags::instance()->edit(['id'=>$param['id']],['desc'=>$param['desc']]);
        if($res === false){
            return Normal::returnCode(Normal::FAIL,'编辑失败');
        }
        return Normal::returnCode(Normal::SUC,'获取成功');
    }

    public function showTags($param){
        $info = GoodsTags::instance()->info(['cid'=>$param['cid'],'show'=>1,'_fields_'=>'id,name,img']);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'未设置小程序标签显示');
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$info);
    }

    public function allTagslists($param){
        $data_lists = GoodsTags::instance()->lists(['cid'=>$param['cid'],'_fields_'=>'id,name']);
        return Normal::returnCode(Normal::SUC,'获取成功',$data_lists);
    }

    public function setShowTags($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'标签ID不能为空');
        }
        $info = GoodsTags::instance()->info(['id'=>$param['id'],'cid'=>$param['cid']]);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'标签不存在');
        }
        GoodsTags::instance()->startTrans();
        try{
            GoodsTags::instance()->where(['cid'=>$param['cid']])->update(['show'=>0,'position'=>0]);
            GoodsTags::instance()->where(['cid'=>$param['cid'],'id'=>$param['id']])->update(['show'=>1,'position'=>1]);
            GoodsTags::instance()->commit();
        }catch (\Exception $e){
            GoodsTags::instance()->rollback();
            return Normal::returnCode(Normal::FAIL,'设置失败');
        }
        return Normal::returnCode(Normal::SUC,'设置成功');
    }
    
    /**
     * 获取标签下拉列表
     * @param array $param
     * @return array
     */
    public function tagListsNoPage(array $param):array{
        $where[] = ['cid','=',$param['cid']];
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['type','=',$param['type']];
        }
        if(isset($param['name']) && $param['name'] != ''){
            $where[] = ['name','like','%'.trim($param['name']).'%'];
        }
        $data_lists = GoodsTags::instance()->where($where)->field('id,type,name,desc')->order('id desc')->select();
        foreach ($data_lists as $key=>$val){
            if($val['type'] == 0){ //供应商标签
                $data_lists[$key]['goods_num'] = Goods::instance()->where(['cid'=>$param['cid']])->where('find_in_set(:supply_tag,supply_tag)',['supply_tag'=>$val['id']])->count(); //供应商标签关联商品数
            }else{
                $data_lists[$key]['goods_num'] = Goods::instance()->where(['cid'=>$param['cid']])->where('find_in_set(:tag,tag)',['tag'=>$val['id']])->count();  //自定义标签关联数
            }
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$data_lists);
    }
}